<?php

/**
 * Application Model DbTables
 *
 * @package Application_Model
 * @subpackage DbTable
 * @author <YOUR NAME HERE>
 * @copyright ZF model generator
 * @license http://framework.zend.com/license/new-bsd     New BSD License
 */

/**
 * Table definition for governeras
 *
 * @package Application_Model
 * @subpackage DbTable
 * @author <YOUR NAME HERE>
 */
class Application_Model_DbTable_Governeras extends Application_Model_DbTable_TableAbstract
{
    /**
     * $_name - name of database table
     *
     * @var string
     */
    protected $_name = 'governeras';

    /**
     * $_id - this is the primary key name
     *
     * @var int
     */
    protected $_id = 'idgoverneras';

    protected $_sequence = true;

    protected $_referenceMap = array(
        'FkGovernerasGoverneras1' => array(
          	'columns' => 'idperegov',
            'refTableClass' => 'Application_Model_DbTable_Governeras',
            'refColumns' => 'idgoverneras'
        ),
        'FkGovernerasTypegovernera1' => array(
          	'columns' => 'idtypegovernera',
            'refTableClass' => 'Application_Model_DbTable_Typegovernera',
            'refColumns' => 'idtypegovernera'
        )
    );
    protected $_dependentTables = array(
        'Governeras',
        'Projetregional',
        'Users'
    );

    public function selectForOptions($idpere="",$lang="fr"){
    
    	/**dans le cas ou on veut retourné au péres*/
    
    	$selector=$this->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
    
    	$selector->setIntegrityCheck(false);
    
    	$selector->columns(array("key"=>"idgoverneras","value"=>"name"));
    	$ispere=1;
    	if(!empty($idpere)){
    		$infopere=explode("|", $idpere);
    		if($infopere[0]=="RP"){
    
    			$constraint=" idtypegovernera = '".$infopere[1]."' ";
    			$constraint.=(!empty($infopere[2])) ? "  AND idperegov='".$infopere[2]."'":" AND idperegov IS NULL ";
    
    			$ispere=(!empty($infopere[2])) ? 1 : 0;
    
    			$selector->where($constraint);
    		}else{
    			$ispere= 1 ;
    			$selector->where("idperegov = '".$idpere."'");
    		}
    
    
    	}else{
    		$ispere= 1 ;
    		$selector->where("idperegov IS NULL ");
    	}
    	//	echo $selector->__toString();
    	return array("ispere"=>$ispere ,"data"=>$this->fetchAll($selector)->toArray());
    
    }

}
